using System;
using System.Data;
using System.Reflection;
using NHibernate;

namespace Serpis
{
	public class Persister : IPersister
	{
		public virtual void Save(object model)
		{		
			ISession session=AppDbConnection.Instance.SessionFactory.OpenSession();
			session.SaveOrUpdate(model);
			session.Flush();
			session.Close();
		}

		public virtual object Load(Type type, object id)
		{
			object model=Activator.CreateInstance(type);
			ISession session=AppDbConnection.Instance.SessionFactory.OpenSession();
			model=session.Get(type,id);
			session.Close();
			return model;
		}
		
		public void Delete(Type type, object id)
		{
			object model=Load(type,id);
			ISession session=AppDbConnection.Instance.SessionFactory.OpenSession();
			session.Delete(model);
			session.Flush();
			session.Close();
		}
	}
}
